package com.fifth.emp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.BusinessInfo;
import com.fifth.common.pojo.entity.Examine;
import com.fifth.common.pojo.entity.Message;
import com.fifth.emp.config.WebUtils;
import com.fifth.emp.feign.BusinessFeign;
import com.fifth.emp.mapper.ExamineMapper;
import com.fifth.emp.service.IExamineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 审查表 服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-08
 */
@Service
public class ExamineServiceImpl extends ServiceImpl<ExamineMapper, Examine> implements IExamineService {

    @Autowired
    private ExamineMapper examineMapper;
    
    @Autowired
    private BusinessFeign businessFeign;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public CommonResult getExamine(Integer currentPage, Integer pageSize, String startTime, String endTime) {
        IPage<Examine> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Examine> wrapper = new QueryWrapper<>();
        wrapper.eq("e.is_deleted",0);
        //,@Param("starttime") String starttime,@Param("endtime") String endtime
        if(StringUtils.hasText(startTime)){
            wrapper.ge("e.gmt_create",startTime);
        }
        if(StringUtils.hasText(endTime)) {
            wrapper.le("e.gmt_create", endTime);
        }
        IPage<Examine> page1 = examineMapper.getExamine(page,wrapper);
        return new CommonResult(2000,"查询成功",page1);
    }

    @Override
    public CommonResult deleteExamine(String id) {
        int i = examineMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",i);
    }

    @Override
    public CommonResult insertExamine(Examine examine) {
        //获取userid
        HttpServletRequest request = WebUtils.getRequest();
        //获取token
        String token = request.getHeader("token");
        //根据token获取userid
        Map userMap = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        String userid;
        if (userMap!=null && userMap.containsKey("user")) {
            JSONObject user = (JSONObject) userMap.get("user");
            userid = user.getString("id");
        }else {
            return new CommonResult(4000,"登录失效,请登录后再发布",null);
        }
        examine.setCheckman(userid);
        if (StringUtils.hasText(examine.getMsg())){
            examine.setMsg(examine.getMsg());
        }
        if (examine.getMsg()==null){
            examine.setMsg("无");
        }
        int insert = examineMapper.insert(examine);

        if (examine.getState().equals("1")){
            businessFeign.updatestate(examine.getBid());
        }

        return new CommonResult(2000,"添加成功",insert);
    }

    @Override
    public CommonResult selectBusiness() {
        List<BusinessInfo> ipshop = businessFeign.ipshop();
        return new CommonResult(2000,"查询成功店铺成功",ipshop);
    }
}
